package com.alex.web.db.domain;

import java.io.Serializable;

/**
 * 自定义分页参数对象
 *
 * 不集成排序条件，因为不分页的查询也会用到排序
 *
 * Created by weijs on 2017/11/2.
 */
public class PageInfo implements Serializable{

    private static final int DEFAULT_PAGE_NO = 0;   //为了兼容JPA，默认页面码为0

    private static final int DEFAULT_PAGE_SIZE =10;

    private int page = DEFAULT_PAGE_NO;

    private int size = DEFAULT_PAGE_SIZE;

    private int startIndex = 0;

    public PageInfo(int page) {
        if (page < DEFAULT_PAGE_NO) {
            page = DEFAULT_PAGE_NO;
        }

        this.page = page;
        this.startIndex = page * size;
    }

    public PageInfo(Integer page) {
        if (null == page || page < DEFAULT_PAGE_NO) {
            page = DEFAULT_PAGE_NO;
        }

        this.page = page;
        this.startIndex = page * size;
    }

    public PageInfo(int page, int size) {
        if (page < DEFAULT_PAGE_NO) {
            page = DEFAULT_PAGE_NO;
        }

        this.page = page;
        this.size = size;
        this.startIndex = page * size;
    }


    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public int getStartIndex() {
        return startIndex;
    }

    public void setStartIndex(int startIndex) {
        this.startIndex = startIndex;
    }

    @Override
    public String toString() {
        return "PageInfo{" +
                "page=" + page +
                ", size=" + size +
                ", startIndex=" + startIndex +
                '}';
    }
}
